//
//  MarriageLeaveInsuranceVC.m
//  BaseProject
//
//  Created by mac on 2021/1/11.
//  Copyright © 2021 王洋. All rights reserved.
//

#import "MarriageLeaveInsuranceVC.h"
#import "BaseScrollView.h"
#import "MLITopView.h"
#import "MLISegementView.h"
#import "MLIBottomView.h"
@interface MarriageLeaveInsuranceVC ()<UIScrollViewDelegate>
@property(nonatomic,strong)BaseScrollView *mainView;
@property(nonatomic,strong)MLITopView *topView;
@property(nonatomic,strong)MLISegementView *segementView;
@property(nonatomic,assign)BOOL canScroll;
@property(nonatomic,strong)MLIBottomView *bottomView;
@end

@implementation MarriageLeaveInsuranceVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createProperty];
    [self createUI];
    
    [self createNaviBarWithTitle:@"婚嫁保险" backButtonBlock:nil];
    [self.naviBarBackButton setImage:[UIImage imageNamed:@"public_back_white"] forState:UIControlStateNormal];//public_back_black
    self.naviBarBottomLineView.hidden = YES;
    self.naviBarView.backgroundColor = ClearColor;
    self.naviBarTitleLab.text = @"";
}
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    _segementView.viewDidAppear = YES;
}
- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    _segementView.viewDidAppear = NO;
}
- (void)dealloc
{
    NSLog(@"MarriageLeaveInsuranceVC ---dealloc");
}
- (void)createProperty
{
    self.navigationController.navigationBarHidden = YES;
    #if RT_INTERACTIVE_PUSH
        self.rt_navigationController.rt_enableInteractivePush = YES;
    #endif
    [self initializationScrollProperty];
}
- (void)createUI
{
    [self mainView];
    [self bottomView];
    [self topView];
    self.segementView.top = self.topView.bottom + 15*ADAPTER_WIDTH;
    self.mainView.contentSize = CGSizeMake(0, self.segementView.bottom + kBottomSafeHeight + 20*ADAPTER_WIDTH);
    [self createNetManager];
}
- (void)createNetManager
{
    WK(weakSelf)
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:[TTUserModel sharedObject].userId forKey:@"userId"];
    [TTHttpManager postRequestWithUrl:[NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,@"/secondaryQueryController/getOrderNumber"] TimeOut:10 PostData:parameters FinishBlock:^(id  _Nonnull responseObject) {
        NSLog(@"%@",responseObject);
        BaseResponse *responModel = [BaseResponse modelWithJSON:responseObject];
        if (responModel.success) {
            weakSelf.bottomView.jsonData = responseObject;
        } else {
            [SVProgressHUD showWithStatus:responModel.message];
            [SVProgressHUD dismissWithDelay:1];
        }
    } FailedBlock:^(NSError * _Nonnull error) {
        [SVProgressHUD showWithStatus:@"请检查网络~"];
        [SVProgressHUD dismissWithDelay:1];
    }];
}
#pragma mark - 滑动模块
- (void)initializationScrollProperty
{
    self.canScroll = YES;
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(leaveTop:) name:@"leaveTop" object:nil];
}
- (void)leaveTop:(NSNotification *)notification
{
    self.canScroll = YES;
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.y > 350*ADAPTER_WIDTH) {
        [self.naviBarBackButton setImage:[UIImage imageNamed:@"public_back_black"] forState:UIControlStateNormal];//public_back_black
        self.naviBarBottomLineView.hidden = NO;
        self.naviBarView.backgroundColor = WhiteColor;
        self.naviBarTitleLab.text = @"婚嫁保险";
    } else {
        [self.naviBarBackButton setImage:[UIImage imageNamed:@"public_back_white"] forState:UIControlStateNormal];//public_back_black
        self.naviBarBottomLineView.hidden = YES;
        self.naviBarView.backgroundColor = ClearColor;
        self.naviBarTitleLab.text = @"";
    }
    
    //设置临界点
    CGFloat maxContentOffsetY = self.topView.height - kTopHeight + 15*ADAPTER_WIDTH;
    if (self.canScroll) {
        if (ceil(scrollView.contentOffset.y) >= ceil(maxContentOffsetY)) {
            self.canScroll = NO;
            [[NSNotificationCenter defaultCenter]postNotificationName:@"arriveTop" object:nil];
        } else {
            self.canScroll = YES;
        }
        self.mainView.showsVerticalScrollIndicator = YES;
    } else {
        [scrollView setContentOffset:CGPointMake(0, maxContentOffsetY)];
        self.mainView.showsVerticalScrollIndicator = NO;
    }
}
#pragma mark - lazy懒加载
- (UIScrollView *)mainView
{
    if (!_mainView) {
        BaseScrollView *view = [[BaseScrollView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - self.bottomView.height)];
        [self.view addSubview:view];
        view.backgroundColor = [UIColor ColorGradientType:ColorGradientTypeHorizontal andWithSize:view.size andWithColors:@[(__bridge id)rgba(254, 121, 82, 1).CGColor,(__bridge id)rgba(222, 81, 43, 1).CGColor]];
        if (@available(iOS 11.0, *))
        {
            view.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        else
        {
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
        view.delegate = self;
        _mainView = view;
    }
    return _mainView;
}
- (MLITopView *)topView
{
    if (!_topView) {
        MLITopView *view = [[MLITopView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 0)];
        [self.mainView addSubview:view];
        _topView = view;
    }
    return _topView;
}
- (MLISegementView *)segementView
{
    if (!_segementView) {
        MLISegementView *view = [[MLISegementView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - kTopHeight - self.bottomView.height)];
        [self.mainView addSubview:view];
        view.parentVC = self;
        WK(weakSelf)
        [view setCallBackNativeJumpBlock:^{
            CGFloat maxContentOffsetY = weakSelf.topView.height - kTopHeight + 15*ADAPTER_WIDTH;
            [weakSelf.mainView setContentOffset:CGPointMake(0, maxContentOffsetY) animated:YES];
        }];
        _segementView = view;
    }
    return _segementView;
}
- (MLIBottomView *)bottomView
{
    if (!_bottomView) {
        MLIBottomView *view = [[MLIBottomView alloc]initWithFrame:CGRectMake(0, HEIGHT - kBottomSafeHeight - 54*ADAPTER_WIDTH, WIDTH, 54*ADAPTER_WIDTH + kBottomSafeHeight)];
        [self.view addSubview:view];
        view.parentVC = self;
        _bottomView = view;
    }
    return _bottomView;
}
@end
